package com.yinhai.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.yinhai.cfg.AppConfig;
import com.yinhai.cfg.LoginConfig;
import com.yinhai.cfg.LoginConfigUtil;
import com.yinhai.constant.UrlConstant;

public class MsgService extends Service {

	private Context thisRef;
	private AQuery aq;

	private Handler handler1 = new Handler();
	private Handler handler2 = new Handler();
	private boolean isRun = false;

	private NotificationManager nm = null;
	private int version;

	@Override
	public void onCreate() {
		super.onCreate();
		thisRef = this;
		aq = new AQuery(this);
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		Log.i("tag", "tuisong Service Create");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (!isRun) {
			isRun = true;
			handler1.postDelayed(msgTask, 1000);
			handler2.postDelayed(surpriseTask, 1000);
		}
		Log.i("tag", "tuisong Service Start");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		isRun = false;
		Log.i("tag", "tuisong Service Stop");
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 消息
	 * */
	private Runnable msgTask = new Runnable() {
		public void run() {
			LoginConfig log = LoginConfigUtil.getConfig(thisRef);
			String phone = log.getUsername();
			SharedPreferences preferences1 = thisRef.getSharedPreferences(
					AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);

			String url = UrlConstant.LOCAL_URI + "findNotifys";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("phone", phone);
			params.put("versionNum", preferences1.getInt("version", 0));
			aq.ajax(url, params, JSONObject.class,
					new AjaxCallback<JSONObject>() {
						@Override
						public void callback(String url, JSONObject json,
								AjaxStatus status) {
							if (json != null) {
								try {
									JSONArray js = json.getJSONArray("data");
									if (js.length() > 0) {
										version = json.getInt("version");
										SharedPreferences preferences = thisRef
												.getSharedPreferences(
														AppConfig.LOGIN_CONFIG,
														Context.MODE_PRIVATE);
										Editor editor = preferences.edit();
										editor.putInt("version", version);
										editor.commit();
										for (int i = 0; i < js.length(); i++) {
											JSONObject js1 = js
													.getJSONObject(i);
											NoteManage.sendNotificationForPush(
													nm,
													thisRef,
													"0",
													js1.getString("NOTIFY_TITLE"),
													js1.getString("NOTIFY_CONTENT"),
													js1.getString("URL"));
										}
									}

								} catch (Exception e) {
									e.printStackTrace();
								}
								if (isRun) {
									handler1.postDelayed(msgTask, 3 * 60 * 1000);
								}
							}
						}
					});

		}
	};

	/**
	 * 集团公告push
	 *
	 * */
	private Runnable surpriseTask = new Runnable() {
		public void run() {
			LoginConfig log = LoginConfigUtil.getConfig(thisRef);
			String phone = log.getUsername();
			String url = UrlConstant.LOCAL_URI + "findNoticeVersion";
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("phone", phone);
			aq.ajax(url, params, JSONObject.class,
					new AjaxCallback<JSONObject>() {
						@Override
						public void callback(String url, JSONObject json,
								AjaxStatus status) {
							if (json != null) {
								try {
									SharedPreferences preferences1 = thisRef
											.getSharedPreferences(
													AppConfig.LOGIN_CONFIG,
													Context.MODE_PRIVATE);
									version = json.getInt("version");
									if (preferences1.getInt("version1", 0) < version) {
										SharedPreferences preferences = thisRef
												.getSharedPreferences(
														AppConfig.LOGIN_CONFIG,
														Context.MODE_PRIVATE);
										Editor editor = preferences.edit();
										editor.putInt("version1", version);
										editor.commit();
										NoteManage.sendNotificationForMsg(nm,
												thisRef);
									}

								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							if (isRun) {
								handler2.postDelayed(surpriseTask,
										3 * 60 * 1000);
							}
						}
					});
		}
	};
}
